//
//  SEGOrderSeiverListCell.m
//  ExcellentHome
//
//  Created by Chiang on 2017/12/25.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGOrderSeiverListCell.h"

@interface SEGOrderSeiverListCell ()

@property (nonatomic, strong) UILabel *catalogLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *modelLabel;
@property (nonatomic, strong) UILabel *unitLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *needFeeLabel;
@property (nonatomic, strong) UIImageView *lineView;

@end

@implementation SEGOrderSeiverListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = kColor_W;
    
    self.lineView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = kColor_L2;
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:imageView];
        imageView;
    });
    
    self.catalogLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_1;
        label.font = kFont_16;
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        label;
    });
    
    self.nameLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_2;
        label.font = kFont_13;
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        label;
    });
    
    self.modelLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_2;
        label.font = kFont_13;
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        label;
    });
    
    self.unitLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_2;
        label.font = kFont_13;
        [self.contentView addSubview:label];
        label;
    });
    
    self.needFeeLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_2;
        label.font = kFont_13;
        [self.contentView addSubview:label];
        label;
    });
    
    self.priceLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColor_2;
        label.font = kFont_13;
        [self.contentView addSubview:label];
        label;
    });
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(1);
    }];
    
    [self.catalogLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.catalogLabel.mas_left);
        make.right.equalTo(self.catalogLabel.mas_right);
        make.top.equalTo(self.catalogLabel.mas_bottom).offset(10);
    }];
    
    [self.modelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.catalogLabel.mas_left);
        make.right.equalTo(self.catalogLabel.mas_right);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(8);
    }];
    
    [self.needFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.catalogLabel.mas_left);
        make.right.equalTo(self.catalogLabel.mas_right);
        make.top.equalTo(self.modelLabel.mas_bottom).offset(8);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.catalogLabel.mas_left);
        make.top.equalTo(self.needFeeLabel.mas_bottom).offset(8);
        make.bottom.mas_equalTo(-12);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.catalogLabel.mas_right);
        make.bottom.mas_equalTo(-12);
    }];
}

- (void)updateWithModel:(SEGOrderPayItemModel *)model {
    NSString *itemCatalog = model.catalogName;
    NSString *itemName = model.itemName;
    NSString *itemModel = model.itemModel;
    self.needFeeLabel.text = nil;
    if (model.type == 2) {
        // 仓库数据
        self.catalogLabel.text = model.itemName;
        self.nameLabel.text = model.itemBrand.length == 0 ? nil : [NSString stringWithFormat:@"品牌：%@", model.itemBrand];
        self.modelLabel.text = model.itemModel.length == 0 ? nil : [NSString stringWithFormat:@"型号：%@", model.itemModel];
        self.needFeeLabel.text = model.verificationType == 1 ? @"是否收费：否" : @"是否收费：是";
        itemName = model.itemBrand;
        itemCatalog = model.itemName;
    } else {
        // [bugfix 27683] 有偿列表type为0展示其他类
        self.catalogLabel.text = model.type == 0 ? @"其他类" : model.catalogName;
        self.nameLabel.text = model.itemName;
        self.modelLabel.text = model.itemModel;
    }
   
    /*
     需求说小数点主要是支持体积、面积、长度、重量，其他的不支持小数
     所以做如下处理：接口返回的数据小数点后不为0则直接展示，为0则展示整数（同安卓的逻辑）
     */
    NSString *useCount = model.useCount;
    if ([model.useCount integerValue] == [model.useCount floatValue]) {
        useCount = [NSString stringWithFormat:@"%ld", (long)[model.useCount integerValue]];
    }
    self.unitLabel.text = [NSString stringWithFormat:@"数量：%@%@",useCount,model.unit];
    NSMutableAttributedString *priceAttributedString = [[NSMutableAttributedString alloc] initWithString:@"小计：" attributes:@{NSFontAttributeName : kFont_13, NSForegroundColorAttributeName : kColor_2}];
    CGFloat price = [model subtotalPrice];
    [priceAttributedString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", price] attributes:@{NSFontAttributeName : kFont_16, NSForegroundColorAttributeName : kColor_S1}]];
    [priceAttributedString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"元" attributes:@{NSFontAttributeName : kFont_13, NSForegroundColorAttributeName : kColor_S1}]];
    self.priceLabel.attributedText = priceAttributedString;
    
    // 没有数据的行需要处理
    CGFloat catalogTop = self.row == 0 ? 15 : 12;
    if (itemCatalog == nil || itemCatalog.length == 0) {
        catalogTop = self.row == 0 ? 5: 2;
    }
    [self.catalogLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(catalogTop);
    }];
    
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.catalogLabel.mas_bottom).offset( (itemName.length == 0) ? 0 : 10);
    }];
    
    [self.modelLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset( (itemModel.length == 0) ? 0 : 8);
    }];
    
    [self.needFeeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.modelLabel.mas_bottom).offset(model.type == 2 ? 8 : 0);
    }];
    
    if (model.type == 0) {
        // 其他类(即自定义的服务)不显示单位
        self.unitLabel.hidden = YES;
    } else {
        // 单位label显示与否根据数量是否为0决定
        self.unitLabel.hidden = [model.useCount integerValue] == 0;
    }
}

-(void)setRow:(NSInteger)row {
    if (row == 0) {
        self.lineView.image = nil;
        self.lineView.backgroundColor = kColor_bg;
    } else {
        self.lineView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_dotted_line")];
        self.lineView.backgroundColor = [UIColor clearColor];
    }
}

@end
